package th.retrofit.lib;

public class Solution76 {

    public String minWindow(String s, String t) {
        int tLen = t.length();
        char[] tArr = t.toCharArray();
        int[] dictionary = new int[128];
        for (char c : tArr){
            dictionary[c]++;
        }
        String res = "";
        for (int i = 0, j = 0, cnt = 0; i < s.length(); i++) {
            char value = s.charAt(i);
            dictionary[value]--;
            if (dictionary[value] >= 0) cnt++;
            while (cnt == tLen && dictionary[s.charAt(j)] < 0) {
                dictionary[s.charAt(j++)]++;
            }
            if (cnt == tLen) {
                if ("".equals(res) || res.length() > i - j + 1) {
                    res = s.substring(j, i + 1);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new Solution76().minWindow("ADOBECODEBANC", "ABC"));
    }

}
